﻿using NodeCanvas.Framework;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    public class AIBaseNode: ActionTask
    {
        protected AIEntity _entity;
        protected bool _openLog;
        
        protected Transform _transform;
        protected Animator _animator;

        protected int _aiUniqueID;
        
        protected bool _isPlayer;

        protected override string OnInit()
        {
            _entity = ownerSystemBlackboard.GetVariable<AIEntity>("AIEntity").value;
            if (_entity && _entity.viewer!=null)
            {
                _transform = _entity.viewer.transform;
                _animator = _entity.viewer.animator;
            }
            _aiUniqueID = ownerSystemBlackboard.GetVariable<int>("UniqueID").value;
            _isPlayer = LibertyAIUtils.PLAYER_AI_ID == _aiUniqueID;
            _openLog = ownerSystemBlackboard.GetVariable<bool>("OpenLog").value;
            return null;
        }

        protected override void PreOnExecute()
        {
            if (_entity == null)
            {
                _entity = ownerSystemBlackboard.GetVariable<AIEntity>("AIEntity").value;
                if (_entity && _entity.viewer != null)
                {
                    _transform = _entity.viewer.transform;
                    _animator = _entity.viewer.animator;
                }
   
                _aiUniqueID = ownerSystemBlackboard.GetVariable<int>("UniqueID").value;
                _isPlayer = LibertyAIUtils.PLAYER_AI_ID == _aiUniqueID;
            }
        }

        protected virtual Vector3 GetPosition()
        {
            return _transform.position;
        }
        
        protected virtual void SetPosition(Vector3 pos)
        {
            if (_isPlayer)
            {
                Vector3 rot = _transform.eulerAngles;
                LibertyAIUtils.PlayerController.PlayerTransmission(pos,rot,false);
            }
            else
            {
                _transform.position = pos;
                _entity.seeker.SetPosition(pos);
                _entity.data.position = pos;
            }
        }

        protected virtual void SetRotation(Quaternion rot)
        {
            if (_isPlayer)
            {
                Vector3 pos = _transform.position;
                LibertyAIUtils.PlayerController.PlayerTransmission(pos,rot.eulerAngles,false);
            }
            else
            {
                _transform.rotation = rot;
                _entity.seeker.Transform.rotation = rot;
                _entity.AssignFixedRotation(rot.eulerAngles);
            }
        }

        public virtual void SetDestination(Vector3 destination)
        {
            if (_isPlayer)
            {
                LibertyAIUtils.PlayerController.SetPlayerDestination(destination);
            }
            else
            {
                _entity.seeker.SetDestination(destination);
                _entity.data.destination = destination;
            }

        }
        
        public virtual void StopAISeeker()
        {
            if (_isPlayer)
            {
            }
            else
            {
                _entity.seeker.Stop();
            }

        }

        public virtual void SetMaxSpeed(float speed)
        {
            if (_isPlayer)
            {
                LibertyAIUtils.PlayerController.SetPlayerNavSpeed(speed);
            }
            else
            {
                //_entity.seeker.SetPriority(Mathf.Min(0.85f, Mathf.Max(0.1f, (speed - 1.2f) * 2f)));
                _entity.seeker.SetPriority(0.5f);
                _entity.seeker.SetMaxSpeed(_entity.propertyDataConfig.move_speed*speed);
                _entity.data.maxSpeed = speed;
                _entity.viewer.animator.SetFloat("Speed", speed);
            }
        }
        
        protected virtual void PlayAnimation(string state)
        {
            foreach (var param in _animator.parameters)
            {
                _animator.ResetTrigger(param.name);
            }
            _animator.SetTrigger(state);
        }


        protected void SetTransformData()
        {
            _entity.data.position = _transform.position;
            // _entity.data.CurrentRotation = _transform.eulerAngles;
        }
    }
}